<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>退住申请记录</h2>
          <el-button
              type="primary"
              icon="el-icon-plus"
              @click="showApplicationForm = true"
          >
            新增退住申请
          </el-button>
        </div>
      </template>

      <!-- 申请记录表格 -->
      <el-table
          :data="applicationList"
          v-loading="loading"
          border
          stripe
          style="width: 100%"
      >
        <el-table-column
            prop="createTime"
            label="申请时间"
            width="180"
        ></el-table-column>
        
        <el-table-column
            prop="status"
            label="状态"
            width="120"
        >
          <template #default="{row}">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column
            prop="expectedDate"
            label="预计退住时间"
            width="150"
        ></el-table-column>
        
        <el-table-column
            prop="processedByName"
            label="处理人"
            width="150"
        >
          <template #default="{row}">
            {{ row.processedByName || '暂未处理' }}
          </template>
        </el-table-column>
        
        <el-table-column
            label="处理备注"
        >
          <template #default="{row}">
            <div style="white-space: pre-wrap;">{{ row.notes || '-' }}</div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
          class="pagination"
          :current-page="pageInfo.pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      ></el-pagination>

      <!-- 申请表单弹窗 -->
      <el-dialog
          title="新建退住申请"
          v-model="showApplicationForm"
          width="600px"
          @closed="form = initForm()"
      >
        <el-form
            :model="form"
            label-width="120px"
        >
          <el-form-item label="申请备注" required>
            <el-input
                v-model="form.reason"
                type="textarea"
                :rows="4"
                placeholder="请输入退住原因"
            ></el-input>
          </el-form-item>

          <el-form-item label="预计退住时间" required>
            <el-date-picker
                v-model="form.expectedDate"
                type="date"
                placeholder="选择日期"
                value-format="YYYY-MM-DD"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submit">提交申请</el-button>
            <el-button @click="showApplicationForm = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {ElMessage} from 'element-plus'
import http from '@/utils/http.js'
import utils from "@/utils/tools.js";

const applicationList = ref([])
const showApplicationForm = ref(false)
const pageInfo = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
})

const initForm = () => ({
  applicantRemark: '',
  expectedDate: '',
  elderId: utils.getCurrentUser().id,
})

const form = ref(initForm())
const loading = ref(false)

const getStatusType = (status) => {
  const statusMap = {
    '0': 'info',    // 待处理
    '1': 'success', // 已通过
    '2': 'danger'   // 已拒绝
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    '0': '待处理',
    '1': '已通过',
    '2': '已拒绝'
  }
  return statusMap[status] || '未知状态'
}

async function getApplications() {
  loading.value = true
  try {
    const res = await http.get('/checkoutApplication/page', {
      params: {
        elderId: utils.getCurrentUser().id,
        pageNum: pageInfo.value.pageNum,
        pageSize: pageInfo.value.pageSize
      }
    })
    applicationList.value = res.data.list
    pageInfo.value.total = res.data.total
  } catch (e) {
    ElMessage.error('获取申请记录失败')
  } finally {
    loading.value = false
  }
}

const submit = () => {
  http.post('/checkoutApplication/add', form.value).then(() => {
    ElMessage.success('申请提交成功')
    getApplications()
    showApplicationForm.value = false
  })
}

const handleSizeChange = (val) => {
  pageInfo.value.pageSize = val
  getApplications()
}

const handleCurrentChange = (val) => {
  pageInfo.value.pageNum = val
  getApplications()
}

onMounted(() => {
  getApplications()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style> 